import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
import { getToken } from '@/utils/auth'

Vue.use(VueRouter)

// 路由配置
const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index'),
    meta: { title: '登录' }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/register/index'),
    meta: { title: '注册' }
  },
  {
    path: '/',
    component: () => import('@/layout/index'),
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '首页', icon: 'el-icon-s-home' }
      },
      {
        path: 'account',
        name: 'Account',
        component: () => import('@/views/account/index'),
        meta: { title: '账号密码管理', icon: 'el-icon-lock' }
      },
      {
        path: 'finance',
        name: 'Finance',
        component: () => import('@/views/finance/index'),
        meta: { title: '收支记录管理', icon: 'el-icon-money' }
      },
      {
        path: 'note',
        name: 'Note',
        component: () => import('@/views/note/index'),
        meta: { title: '笔记管理', icon: 'el-icon-notebook-1' }
      },
      {
        path: 'sticky',
        name: 'Sticky',
        component: () => import('@/views/sticky/index'),
        meta: { title: '便签墙', icon: 'el-icon-s-grid' }
      },
      {
        path: 'bookmark',
        name: 'Bookmark',
        component: () => import('@/views/bookmark/index'),
        meta: { title: '网络地址管理', icon: 'el-icon-link' }
      },
      {
        path: 'file',
        name: 'File',
        component: () => import('@/views/file/index'),
        meta: { title: '文件管理', icon: 'el-icon-folder' }
      },
      {
        path: 'profile',
        name: 'Profile',
        component: () => import('@/views/profile/index'),
        meta: { title: '个人中心', icon: 'el-icon-user' }
      },
      {
        path: 'operationLog',
        name: 'OperationLog',
        component: () => import('@/views/operationLog/index'),
        meta: { title: '操作日志', icon: 'el-icon-document' }
      }
    ]
  },
  {
    path: '*',
    redirect: '/404',
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error/404'),
    hidden: true
  }
]

const router = new VueRouter({
  routes
})

// 路由守卫
router.beforeEach(async (to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 日常信息管理系统` : '日常信息管理系统'
  
  // 获取token
  const hasToken = getToken()
  
  if (hasToken) {
    if (to.path === '/login') {
      // 已登录，跳转到首页
      next({ path: '/' })
    } else {
      // 判断是否已获取用户信息
      const hasUserInfo = store.getters.userId
      if (hasUserInfo) {
        next()
      } else {
        try {
          // 获取用户信息
          await store.dispatch('user/getInfo')
          next()
        } catch (error) {
          // 获取用户信息失败，清除token并跳转到登录页
          await store.dispatch('user/logout')
          next(`/login?redirect=${to.path}`)
        }
      }
    }
  } else {
    // 未登录，可以访问白名单页面
    if (['/login', '/register', '/404'].indexOf(to.path) !== -1) {
      next()
    } else {
      // 跳转到登录页
      next(`/login?redirect=${to.path}`)
    }
  }
})

export default router 